package 力扣_树算法.树编号;

/**222. 完全二叉树的节点个数
 * @author zx
 * @create 2022-04-11 20:31
 */
public class Num222 {
    /**
     关于完全二叉树的节点编号
     若根节点从1开始编号,设父节点的编号为K,则左子树2K,右子树为2K + 1
     */
    int res = 0;
    public int countNodes(TreeNode root) {
        helper(root,1);
        return res;
    }
    private int helper(TreeNode root, int index){
        if(root == null){
            return res;
        }
        res = Math.max(res,index);
        helper(root.left,index * 2);
        helper(root.right,index * 2 + 1);
        return res;
    }
}
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}